package kr.hkit.androidcomplete03.ch20;

import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class FragmentTest extends Activity {

	public final static String TAG = "FragmentCycle";
	public static int num = 0;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragementtest);
	}
	
	public static class CounterFragment extends Fragment{

		TextView txtcounter;
		@Override
		public void onCreate(Bundle savedInstanceState) {
			Log.d(TAG, "          Fragement - onCreate()");
			super.onCreate(savedInstanceState);
		}

		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			Log.d(TAG, "          Fragement - onActivityCreated()");
			super.onActivityCreated(savedInstanceState);
		}

		@Override
		public void onStart() {
			Log.d(TAG, "          Fragement - onStart()");
			super.onStart();
		}

		@Override
		public void onResume() {
			Log.d(TAG, "          Fragement - onResume()");
			super.onResume();
		}

		@Override
		public void onPause() {
			Log.d(TAG, "          Fragement - onPause()");
			super.onPause();
		}

		@Override
		public void onStop() {
			Log.d(TAG, "          Fragement - onStop()");
			super.onStop();
		}

		@Override
		public void onDestroy() {
			Log.d(TAG, "          Fragement - ondestory()");
			super.onDestroy();
		}

		@Override
		public void onDetach() {
			Log.d(TAG, "          Fragement - onDetach()");
			super.onDetach();
		}
		
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			Log.d(TAG, "          Fragement - onCreateView()");
			
			View root = inflater.inflate(R.layout.ch20_counterfragment, container, false);
			Button btnIncrease = (Button) root.findViewById(R.id.btnincrease);
			txtcounter = (TextView) root.findViewById(R.id.txtcounter);
			
			if(savedInstanceState != null){
				txtcounter.setText(Integer.toString(savedInstanceState.getInt("counter")));
			}
			
			Bundle bundle = getArguments();
			if(bundle != null){
				txtcounter.setText(Integer.toString(bundle.getInt("start")));
			}
			
			btnIncrease.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					num = Integer.parseInt(txtcounter.getText().toString());
					txtcounter.setText(Integer.toString(num+1));
				}
			});
			return root;
		}

		@Override
		public void onSaveInstanceState(Bundle outState) {
			super.onSaveInstanceState(outState);
			
			Log.d("TAG", outState.toString());
			int a = Integer.parseInt(txtcounter.getText().toString());
			outState.putInt("counter", a);
		}
		
		
	}
	
}
